package com.kelong.front.controller;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.kelong.front.service.UserService;
import com.kelong.front.service.impl.UserServiceImpl;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.kelong.front.validate.Test;
import java.util.*;
import com.kelong.front.service.AjaxResult;
import com.kelong.front.vo.SystemLoginVo;


import com.kelong.front.pojo.User;

/**
 * restful 接口
 * 示例代码
 */
@RestController
public class ApiController {

    @Resource
    UserService userService;


    /**
     * 添加用户
     *
     * @return
     */
    @PostMapping("/addUser")
    public AjaxResult<SystemLoginVo> addUser(@Validated() @RequestBody Test loginsValidate) {
        SystemLoginVo vo = userService.addUser( loginsValidate);
       return AjaxResult.success(vo);
    }

    /**
     * 修改用户
     *
     * @return
     */
    @GetMapping("/updateUser")
    public boolean updateUser() {
        User model = new User();
        long id = 3;
        model.setId(id);
        model.setName("787878uj");
        model.setPassword("ooooo");
        model.setAge(8);
       return userService.deleteUserById(model);
    }

    /**
     * 删除单个用户
     *
     * @return
     */
    @GetMapping("/deleteById")
    public boolean deleteById() {
        User model = new User();
        long id = 4;
        model.setId(id);
        return userService.deleteUserById(model);
    }

    /**
     * 批量删除用户
     *
     * @return
     */
    @GetMapping("/deleteUsers")
    public boolean deleteUsers() {
        long id1 = 1;
        long id2 = 2;
        List<Long> ids = Arrays.asList(id1, id2);
        return userService.deleteUser(ids);
    }

    /**
     * 查询单个用户
     *
     * @return
     */
    @GetMapping("/selectUserById")
    public Optional<User> selectUserById() {
        User model = new User();
        long id = 3;
        model.setId(id);
        return userService.findUserById(model);
    }

    /**
     * 查询多个用户
     *
     * @return
     */
    @GetMapping("/selectUsersByIds")
    public List<User> selectUsersByIds() {
        long id1 = 3;
        long id2 = 102;
        List<Long> ids = Arrays.asList(id1, id2);
        return userService.findUsersByIds(ids);
    }


    /**
     * 根据年龄查询多个用户
     *
     * @return
     */
    @GetMapping("/selectUsersByAge")
    public List<User> selectUsersByAge() {
        int age=20;
        return userService.findByAge(age);
    }


    /**
     * 返回版本信息
     * json 数据
     *
     * @return
     */
    @GetMapping("/version")
    public Map<String, Object> version() {
        Map<String, Object> console = new LinkedHashMap<>();
        // 版本信息
        Map<String, Object> version = new LinkedHashMap<>();
        Map<String, String> channel = new LinkedHashMap<>();
        channel.put("gitee", "https://gitee.com/likeadmin/likeadmin_java");
        version.put("channel", channel);
        console.put("version", version);

        return console;
    }
}